<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="dgg" uri="/dgg-tags" %>
<div class="body-main">
    <div style="display: none;">
        <input id="generalType" value="${generalType}"/>
    </div>
    <form id="credentialForm" action="">
        <table class="add-table form-table">
            <tr>
                <th><span class="required">地区</span></th>
                <td>
                    <div class="bus-plugIn" id="certificateAreas">
                        <input type="hidden" readonly name="certificateAreaId" class="plug-in-msg"/>
                        <input type="hidden" readonly name="certificateArea" class="plug-in-code" value="${empty warehousing.certificateArea ? warehousing.areaProvince :warehousing.certificateArea}"/>
                        <input type="text" readonly placeholder="请选择" class="chose-plugIn layui-input" name="areaName" id="certificateName" value="<dgg:tbcode code='${warehousing.areaProvince}'/> <c:if test="${!empty warehousing.certificateArea}">/<dgg:tbcode code='${warehousing.certificateArea}'/></c:if>"/>
                        <div class=" bus-picker">
                            <div class="bus-picker-tab">
                                <a href="javascript:void(0);" class="active">请选择</a>
                            </div>
                            <div class="bus-picker-content"></div>
                        </div>
                    </div>
                </td>
            </tr>
            <tr>
                <th><span class="required">证书类型：</span></th>
                <td>
                    <select class="select-style" name="certificateType" id="certificateType" onchange="loadCertificateType('240px','#certificateType','${generalType}',1,'#certificateSpecialtys',0,'#certificateLevel')">
                        <option value="">请选择</option>
                    </select>
                </td>
            </tr>
            <tr>
                <th><span class="required">证书专业：</span></th>
                <td>
                    <div class="layui-input-inline">
                        <select class="select-style" id="certificateSpecialtys" name="certificateSpecialtys"
                                style="width:240px">
                            <option value="">请先选择证书类型</option>
                        </select>
                    </div>
                </td>
            </tr>
            <tr>
                <th><span class="required">证书等级：</span></th>
                <td>
                    <div class="layui-input-inline">
                        <select class="select-style" name="certificateLevel" id="certificateLevel" style="width:240px">
                            <option value="">请先选择证书类型</option>
                        </select>
                    </div>
                </td>
            </tr>
            <tr>
                <th><span class="required">库存数量</span></th>
                <td>
                    <input type="hidden" id="id" name="id" value="${warehousing.id}">
                    <input name="stockQuantity" id="stockQuantity" style="width: 240px;border: 1px solid #e1d0d0;" placeholder="请输入库存数量" oninput = "value=value.replace(/[^\d]/g,'')" value="${warehousing.stockQuantity}">
                </td>
            </tr>
        </table>
    </form>
</div>
<script type="text/javascript"
        src="${basePath}/static/js/zzsc/common/dgg.verify.js?v=${version}"></script>
<script type="text/javascript">

    //属性初始化
    $(function () {
        loadCertificateType('240px', '#certificateType', $("#generalType").val(), 0);
        setTimeout("loadCertificateType('240px', '#certificateType', $(\"#generalType\").val(), 1,'#certificateSpecialtys',0,'#certificateLevel')",200);
        setTimeout(function(){
            $("#certificateSpecialtys").change(function() {
                var certificateSpecialtys = $("#certificateSpecialtys").val();
                if (certificateSpecialtys) {
                    if (certificateSpecialtys.indexOf("isAll") != -1) {
                        setTimeout("loadCertificateType1('240px', '#certificateType', $(\"#generalType\").val(), 1,'#certificateSpecialtys',0,'#certificateLevel')");
                    }
                }
            });
        },500);
    });
    function loadCertificateType(length, obj, generalType, type, certificateSpecialtyObj, isLoadLevel, certificateLevelObj) {
        if (checkIsEmpty(type)) {
            DGG.Errors("请传入联动类型！");
            return false;
        }
        if (type === 0) {
            $(obj).find("option").remove();
            $(obj).append("<option value=''>请选择</option>");
            $.post(sysInfo.basePath + "/zs/certificate_type/get_certificate_type.do", {
                parentId: 0,
                generalType: generalType
            }, function (result) {
                if (result.code == 0) {
                    $.each(result.data, function (i, item) {
                        if(item.id == ${warehousing.certificateType}) {
                            $(obj).append("<option value='" + item.id + "' data-level='" + item.certificateLevel + "' selected>" + item.certificateName + "</option>");
                        }else{
                            $(obj).append("<option value='" + item.id + "' data-level='" + item.certificateLevel + "'>" + item.certificateName + "</option>");
                        }
                    });
                } else {
                    DGG.Errors(result.msg);
                }
            });
            $(obj).select2({
                placeholder: "请选择",
                allowClear: true,
                width: length,
                language: "zh-CN"
            });
        } else if (type === 1) {
            // 验证证书类型是否已经选择
            var certificateTypeId = $(obj + " option:selected").val();
            if (!checkIsEmpty(certificateTypeId)) {
                $(certificateSpecialtyObj).find("option").remove();
                $.post(sysInfo.basePath + "/zs/certificate_type/get_certificate_type.do", {
                    parentId: certificateTypeId,
                    generalType: generalType
                }, function (result) {
                    if (result.code == 0) {
                        $(certificateSpecialtyObj).val('').trigger('change');
                        $(certificateSpecialtyObj).append("<option value=''>请选择</option>");
                        $(certificateSpecialtyObj).append("<option value='isAll'>全部</option>");
                        $.each(result.data, function (i, item) {
                            var str = '${warehousing.certificateSpecialtys}';
                            if(str.indexOf(item.id) != -1){
                                $(certificateSpecialtyObj).append("<option value='" + item.id + "' data-level='" + item.certificateLevel + "' selected='selected'>" + item.certificateName + "</option>");
                            }else{
                                $(certificateSpecialtyObj).append("<option value='" + item.id + "' data-level='" + item.certificateLevel + "'>" + item.certificateName + "</option>");
                            }
                        });
                    } else {
                        DGG.Errors(result.msg);
                    }
                });
                $(certificateSpecialtyObj).select2({
                    placeholder: "请选择",
                    allowClear: false,
                    width: length,
                    multiple:true,
                    language: "zh-CN"
                });

                if (!checkIsEmpty(isLoadLevel) && isLoadLevel === 0) {
                    // 加载证书等级
                    var levelStrs = $(obj + " option:selected").attr("data-level");
                    $(certificateLevelObj).find("option").remove();
                    $(certificateLevelObj).append("<option value=''>请选择</option>");
                    var levels;
                    if (!checkIsEmpty(levelStrs)) {
                        levels = levelStrs.split(",");
                    }
                    if (checkIsEmpty(levels)) {
                        $(certificateLevelObj).append("<option value=''>暂无等级</option>");
                    } else {
                        $.post(sysInfo.basePath + '/zzsc/common/translate_code.do', {codes: levelStrs}, function (result) {
                            $.each(result.data, function (i, item) {
                                if(item.code == '${warehousing.certificateLevel}'){
                                    $(certificateLevelObj).append('<option value="' + item.code + '" selected>' + item.name + '</option>');

                                }else{
                                    $(certificateLevelObj).append('<option value="' + item.code + '">' + item.name + '</option>');
                                }

                            });
                        });
                    }
                    $(certificateLevelObj).select2({
                        placeholder: "请选择",
                        allowClear: true,
                        width: length,
                        language: "zh-CN"
                    });
                }

            } else {
                // DGG.Errors("请先选择证书类型！");
                $(certificateLevelObj).find("option").remove();
                $(certificateLevelObj).append("<option value=''>请选择</option>");
                $(certificateSpecialtyObj).find("option").remove();
                $(certificateSpecialtyObj).append("<option value=''>请选择</option>");
                return false;
            }
        }
    }
    function loadCertificateType1(length, obj, generalType, type, certificateSpecialtyObj, isLoadLevel, certificateLevelObj) {
        if (checkIsEmpty(type)) {
            DGG.Errors("请传入联动类型！");
            return false;
        }
        if (type === 0) {
            $(obj).find("option").remove();
            $(obj).append("<option value=''>请选择</option>");
            $.post(sysInfo.basePath + "/zs/certificate_type/get_certificate_type.do", {
                parentId: 0,
                generalType: generalType
            }, function (result) {
                if (result.code == 0) {
                    $.each(result.data, function (i, item) {
                        if(item.id == ${warehousing.certificateType}) {
                            $(obj).append("<option value='" + item.id + "' data-level='" + item.certificateLevel + "' selected>" + item.certificateName + "</option>");
                        }else{
                            $(obj).append("<option value='" + item.id + "' data-level='" + item.certificateLevel + "'>" + item.certificateName + "</option>");
                        }
                    });
                } else {
                    DGG.Errors(result.msg);
                }
            });
            $(obj).select2({
                placeholder: "请选择",
                allowClear: true,
                width: length,
                language: "zh-CN"
            });
        } else if (type === 1) {
            // 验证证书类型是否已经选择
            var certificateTypeId = $(obj + " option:selected").val();
            if (!checkIsEmpty(certificateTypeId)) {
                $(certificateSpecialtyObj).find("option").remove();
                $.post(sysInfo.basePath + "/zs/certificate_type/get_certificate_type.do", {
                    parentId: certificateTypeId,
                    generalType: generalType
                }, function (result) {
                    if (result.code == 0) {
                        $(certificateSpecialtyObj).append("<option value=''>请选择</option>");
                        $(certificateSpecialtyObj).append("<option value='isAll' selected>全部</option>");
                        $.each(result.data, function (i, item) {
                            $(certificateSpecialtyObj).append("<option value='" + item.id + "' data-level='" + item.certificateLevel + "'>" + item.certificateName + "</option>");

                        });
                    } else {
                        DGG.Errors(result.msg);
                    }
                });
                $(certificateSpecialtyObj).select2({
                    placeholder: "请选择",
                    allowClear: false,
                    width: length,
                    multiple:true,
                    language: "zh-CN"
                });

                if (!checkIsEmpty(isLoadLevel) && isLoadLevel === 0) {
                    // 加载证书等级
                    var levelStrs = $(obj + " option:selected").attr("data-level");
                    $(certificateLevelObj).find("option").remove();
                    $(certificateLevelObj).append("<option value=''>请选择</option>");
                    var levels;
                    if (!checkIsEmpty(levelStrs)) {
                        levels = levelStrs.split(",");
                    }
                    if (checkIsEmpty(levels)) {
                        $(certificateLevelObj).append("<option value=''>暂无等级</option>");
                    } else {
                        $.post(sysInfo.basePath + '/zzsc/common/translate_code.do', {codes: levelStrs}, function (result) {
                            $.each(result.data, function (i, item) {
                                if(item.code == '${warehousing.certificateLevel}'){
                                    $(certificateLevelObj).append('<option value="' + item.code + '" selected>' + item.name + '</option>');

                                }else{
                                    $(certificateLevelObj).append('<option value="' + item.code + '">' + item.name + '</option>');
                                }

                            });
                        });
                    }
                    $(certificateLevelObj).select2({
                        placeholder: "请选择",
                        allowClear: true,
                        width: length,
                        language: "zh-CN"
                    });
                }

            } else {
                // DGG.Errors("请先选择证书类型！");
                $(certificateLevelObj).find("option").remove();
                $(certificateLevelObj).append("<option value=''>请选择</option>");
                $(certificateSpecialtyObj).find("option").remove();
                $(certificateSpecialtyObj).append("<option value=''>请选择</option>");
                return false;
            }
        }
    }

</script>
</body>
</html>
